package com.zebra.remoting.config;


import com.zebra.common.ZebraBaseMessage;
import com.zebra.remoting.protocol.CommandProfile;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 *  TMS 中途启动 需要向Gateway 发消息 让网关链接TMS
 * 2， TMS
 * 3， 返回CMDS 列表
 */
@Data
@ToString
@NoArgsConstructor
public class GwRegisterMessage extends ZebraBaseMessage<GwRegisterMessage> {
    private  ServerInfoMessage tms;
    private List<CommandProfile> cmdList;

    @Override
    public void write(ByteBuf b) {

        tms.write(b);
        writeInt(b, cmdList.size());
        for (CommandProfile cp : cmdList){
            cp.write(b);
        }

    }

    @Override
    public GwRegisterMessage read(ByteBuf b) {
       this.tms = new ServerInfoMessage().read(b);
       int size = readInt(b);
       List<CommandProfile> list = new ArrayList<>(size);
       for (int i=0; i<size; i++){
           list.add(new CommandProfile().read(b));
       }
       this.cmdList = list;
       b.release();
       return this;
    }
}
